package testtiles;

import butines.core.math2d.Vector2D;
import butines.core.math2d.Vector2DUtils;
import butines.game.AbstractState;

public class UnitState extends AbstractState<Unit> {
	
	protected boolean isStayState(Unit unit) {
		return unit.getStateMachine().inState(UnitStayState.class);
	}
	
	protected boolean isHoldState(Unit unit) {
		return unit.getStateMachine().inState(UnitHoldState.class);
	}
	
	protected void doStay(Unit unit) {
		unit.changeState(UnitStayState.getInstance());
	}
	
	protected void doWait(Unit unit) {
		unit.changeState(UnitWaitState.getInstance());
	}
	
	protected boolean atTargetPoint(Unit unit) {
		return unit.atTargetPoint();
	}
	
	protected void arrivelToTargetPoint(Unit unit) {
		unit.getSteering().arrivel(unit.getTargetPoint());
	}
	
	protected void flee(Unit unit, Vector2D target) {
		unit.getSteering().flee(target);
	}
	
	protected void avoidance(Unit unit, Unit obstacle) {
		unit.getSteering().avoidance(obstacle);
	}

	protected void braking(Unit unit) {
		unit.getSteering().braking(0.7f);
	}
	
	protected boolean isMoving(Unit unit) {
		return (unit.getSpeed() > 0.1);
	}

	protected boolean isObstructedTargetPoint(Unit unit, Unit obstacle) {
		if (isHoldState(obstacle)) {
			return false;
		}
		Vector2D obs = obstacle.getPosition();
		Vector2D pt = unit.getTargetPoint();
		float radius = obstacle.getRadiusBounding() + unit.getRadiusBounding() ; 
		float distance = Vector2DUtils.distance(obs, pt);
		return (radius >= distance);
	}
	
}
